// 发表评论组件
import React, { Component, useState, useRef } from 'react';
import avatar from '../images/avatar.jpeg'; // 导入图片

const Forms = (props) => {
  const [txt, setTxt] = useState('xxx');

  const txtRef = useRef(null);

  // // 设置文本状态
  const handleTxt = (e) => {
    setTxt(e.target.value);
  };

  const handleSubmit = () => {
    console.log(props);
    // 回传数据给父组件
    props.addComment(txt);
    // 清空内容
    setTxt('');
    // 设置光标
    txtRef.current.focus();
  };

  return (
    // 发表评论区
    <div className="comment-send-container">
      <div>
        <img className="user-head" src={avatar} alt="头像" />
      </div>
      <div className="comment-send">
        <div className="textarea-container">
          <textarea
            ref={txtRef}
            cols="80"
            rows="5"
            value={txt}
            onChange={handleTxt}
            placeholder="发条友善的评论"
            className="ipt-txt"
          />
          <button className="comment-submit" onClick={handleSubmit}>
            发表评论
          </button>
        </div>
        <div className="comment-emoji">
          <i className="face"></i>
          <span className="text">表情</span>
        </div>
      </div>
    </div>
  );
};

class Forms1 extends Component {
  state = {
    txt: 'xxx',
  };

  txtRef = React.createRef();

  // 设置文本状态
  handleTxt = (e) => {
    this.setState({
      txt: e.target.value,
    });
  };

  handleSubmit = () => {
    console.log(this.props);
    // 回传数据给父组件
    this.props.addComment(this.state.txt);
    // 清空内容
    this.setState({
      txt: '',
    });
    // 设置光标
    this.txtRef.current.focus();
  };

  render() {
    return (
      // 发表评论区
      <div className="comment-send-container">
        <div>
          <img className="user-head" src={avatar} alt="头像" />
        </div>
        <div className="comment-send">
          <div className="textarea-container">
            <textarea
              ref={this.txtRef}
              cols="80"
              rows="5"
              value={this.state.txt}
              onChange={this.handleTxt}
              placeholder="发条友善的评论"
              className="ipt-txt"
            />
            <button className="comment-submit" onClick={this.handleSubmit}>
              发表评论
            </button>
          </div>
          <div className="comment-emoji">
            <i className="face"></i>
            <span className="text">表情</span>
          </div>
        </div>
      </div>
    );
  }
}

export default Forms;
